'this is a string'
'this is a string'
len
¶len('word and word')
13
You can use len
to get the length of a string.
'fire' + 'place'
'fireplace'
'yo' * 2
'yoyo'
'nan ' * 16 + 'batman!'
'nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan batman!'
+=
¶message = 'Hello'
message = message + ' world!'
message
'Hello world!'
message = 'Hello'
message += ' world!'
message
'Hello world!'
for letter in 'this is a string':
print(letter)
t h i s i s a s t r i n g
'a'.isalpha(), '8'.isalpha()
(True, False)
'abcdefg'.isalpha(), 'abc1234'.isalpha(), 'abc!'.isalpha()
(True, False, False)
'a'.isdigit(), '8'.isdigit()
(False, True)
'12345'.isdigit(), '12345pi'.isdigit(), '123.456'.isdigit()
(True, False, False)
'a'.isalnum(), '8'.isalnum()
(True, True)
'12345'.isalnum(), '12345pi'.isalnum(), '123.456'.isalnum()
(True, True, False)
'a'.isspace(), '8'.isspace(), ' '.isspace()
(False, False, True)
'A'.islower(), 'A'.isupper()
(False, True)
'a'.islower(), 'a'.isupper(), '9'.islower(), '9'.isupper()
(True, False, False, False)
'a'.upper(), 'a'.lower()
('A', 'a')
'A'.upper(), 'A'.lower()
('A', 'a')
characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_+=[]{}"\'|:;,./?<> \t\n'
# isalpha
alphas = ''
for character in characters:
if character.isalpha():
alphas += character
print(alphas)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
# isdigit
digits = ''
for character in characters:
if character.isdigit():
digits += character
print(digits)
0123456789
# isalnum
alphanumeric = ''
for character in characters:
if character.isalnum():
alphanumeric += character
print(alphanumeric)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
# isspace
spaces = ''
for character in characters:
if character.isspace():
spaces += character
print(spaces)
# isspace
spaces = []
for character in characters:
if character.isspace():
spaces.append(character)
print(spaces)
for space in spaces:
print(f'>>{space}<<')
[' ', '\t', '\n'] >> << >> << >> << hello world!
# other stuff
symbols = ''
for character in characters:
if not character.isspace() and not character.isalnum():
symbols += character
print(symbols)
`~!@#$%^&*()-_+=[]{}"'|:;,./?<>
# upper and lower
uppers = ''
lowers = ''
for character in characters:
if character.isupper():
uppers += character
elif character.islower():
lowers += character
print(uppers)
print(lowers)
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
Write a function that replaces all space characters with dashes.
def no_spaces(text: str) -> str:
"""Replace all space characters with dashes"""
new_string = ''
for letter in text:
if letter.isspace():
letter = '-'
new_string += letter
return new_string
new_string = ''
for c in text:
if c.isspace():
new_string += '-'
else:
new_string += c
return new_string
print(no_spaces('BYU is the place to be.'))
BYU-is-the-place-to-be.
message = """This is a long,
multiline "string".
It has multiple lines.
That is what "multiline" means. :)"""
print(message)
print()
print(no_spaces(message))
This is a long, multiline "string". It has multiple lines. That is what "multiline" means. :) This-is-a-long,-multiline-"string".-It-has-multiple-lines.-That-is-what-"multiline"-means.-:)
print(no_spaces('Goodbye spaces \t tabs \n and newlines'))
Goodbye-spaces---tabs---and-newlines
Write a function that replaces every digit in a string with ?
def no_numbers(text: str) -> str:
"""Replace every digit with ?"""
new_string = ''
for character in text:
if character.isdigit():
character = '?'
new_string += character
return new_string
no_numbers('There were 7 people.')
'There were ? people.'
no_numbers('15 out of 25 have more than 17.3% contamination.')
'?? out of ?? have more than ??.?% contamination.'
no_numbers('2 + 2 = 5, for large values of 2.')
'? + ? = ?, for large values of ?.'
Add up all the digits found in a string.
def find_digits(text: str) -> str:
"""Return a string of just the digits"""
digits = ''
for char in text:
if char.isdigit():
digits += char
return digits
def turn_to_ints(digits: str) -> list[int]:
"""Turn each digit in the string to an int. Return a list of int."""
numbers = []
for digit in digits:
numbers.append(int(digit))
return numbers
# return [int(n) for n in digits]
def add_digits(text: str) -> int:
"""Add all the digits found in the `text`.
>>> add_digits('123foo')
6
"""
digits = find_digits(text)
numeric_digits = turn_to_ints(digits)
total = sum(numeric_digits)
return total
add_digits('123foo')
6
add_digits('10 students ate 6 oranges and 42 students ate 7 pears.')
20
Write a program that "organizes" user input.
"Organized" text means that all the characters are reordered to this sequence:
organize.py
¶Text: Hello, what is your name?
ellowhatisyournameH,?
Text: BYU is my favorite school!
ismyfavoriteschoolBYU!
Text: 3.14159 is a loose approx. for PI.
isalooseapproxforPI314159...
Text:
'foo' + 'bar'
, 'BYU! ' * 5
.isalpha()
, .isdigit()
, .isalnum()
, .isspace()
, .isupper()
, .islower()
.upper()
, .lower()
+=